<?php
//Connection statement
require_once('../Connections/cn.php');

require_once('../includes/functions.inc.php');


require_once('../includes/tng/tNG.inc.php');
require_once('perdidaFn.php');
$isPago=0;

// begin Recordset

if (isset($_POST['mm_insert2']) && $_POST['mm_insert2'] =='form2' ){
	$valores = $_POST['valor'];
	$materiales = $_POST['chbDetalle'];
	$pagados =$_POST['pagado'];
	$codigoMatPag=$_POST['codMatPag'];
	$codigoMatPer=$_POST['codMatPer'];
	$userPag=$_POST['userPag'];
	$userPer=$_POST['userPer'];
	$detPag=$_POST['detPag'];
	$detPer=$_POST['detPer'];
	

	
	
	
	for ($i =0 ; $i < count($codigoMatPer);$i++)	{
		//material perdido
		if ($valores[$i]!=""){
			ingresarEvento($codigoMatPer[$i]);
			$evento = ReturnEventoCod();			
			ingresarMulta($evento,$userPer[$i],(int)$valores[$i]);
			update_detallePrestamo($detPer);
			echo "Material Registrado";
		}
		
		
	}
	for ($i =0 ; $i < count($codigoMatPag);$i++)	{
		
		if ($pagados[$i]!=""){
			
			updateMulta($pagados[$i]);
			echo "Material Cancelado";
		
		}
	}	
	
	
}

if (isset($_POST['mm_insert']) && $_POST['mm_insert'] =='form1' ){
	$rut_persona = $_POST['rut_persona'];

	$query_rsMaterial = sprintf("SELECT * FROM vs_material WHERE rut_persona = '%s' and estado_detalleprestamo in (2,3,5) and (estado_multa is null or estado_multa =0)",$rut_persona);
	$rsMaterial = $cn->SelectLimit($query_rsMaterial) or die($cn->ErrorMsg());
	$totalRows_rsMaterial = $rsMaterial->RecordCount();
	
	
	
	
	
}
// end Recordset
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php //PHP ADODB document - made with PHAkt 3.6.0?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript" language="javascript">
function confirmarReservas(e){
	
	var cadena = "";
	var frm = document.getElementById("form2")
	frm.submit();
	/*var counter=0;
	for (var i=0;i<frm.length;i++)
	  {
	  	if (frm.elements[i].name=='valor[]'){
	  	  if (frm.elements[i].value==""){
		  	counter++;
		  }
		}
	  }
	 if (counter>0){
	 	alert("Debe seleccionar por lo menos un Material");			
	 } else {
	 	
		frm.submit();
		
	 }*/

	
} 


</script>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
  Rut Usuario 
  <input name="rut_persona" type="text" id="rut_persona">
  <input name="Buscar" type="submit" id="Buscar" value="Submit">
  <input type="hidden" name="mm_insert" value="form1" />
</form>
<?php
if (isset($_POST['mm_insert']) && $_POST['mm_insert'] =='form1' ){
?>
<form id="form2" name="form2" method="post" action="" onSubmit="return false">
	<table  border="1" cellpadding="2" cellspacing="2">
	  <tr>
		<td>fecha</td>
		<td>Coleccion</td>
		<td>Area</td>
		<td>T&iacute;tulo</td>
		<td>Portada</td>
		<td>Estado</td> 		
		<td>Valor / pagado ?</td>
		
	  </tr>
	  <?php
	  while (!$rsMaterial->EOF) {
	?>
		<tr>
		  <td><?php echo $rsMaterial->Fields('fecha_entrega'); ?></td>
	      <td><?php echo $rsMaterial->Fields('nombre_coleccion'); ?>
	       </td>
		  <td><?php echo $rsMaterial->Fields('describe_area'); ?></td>
		  <td><?php echo $rsMaterial->Fields('titulo'); ?></td>
		  <td><img src="../imagenes/caratulas/<?php echo $rsMaterial->Fields('portada'); ?>" alt="portada" name="portada" width="50" height="65" id="portada"></td>
		  
		  <td><?php
		  	if ($rsMaterial->Fields('estado_multa')==""){
				echo "sin perdida";
				$isPago=0;			
			} else {
				echo "inpago";
				$isPago=1;
			}		  
		  ?>		  
		  </td>
		  <td>
		  <?php
		  	if ($isPago == 0){
			?>
		  <input type="text" name="valor[]" value="<?php echo $rsMaterial->Fields('monto_multa')?>" size="10">
		   <input type="hidden" name="codMatPer[]" value="<?php echo $rsMaterial->Fields('id_material'); ?>" />
		   <input type="hidden" name="userPer[]" value="<?php echo $rsMaterial->Fields('id_usuario'); ?>" />
		   <input type="hidden" name="detPer[]" value="<?php echo $rsMaterial->Fields('id_detalleprestamo'); ?>" />
		   
		  	<?php
			} else {
			?>
				<input type="checkbox" name="pagado[]" value="<?php echo $rsMaterial->Fields('id_multa')?>">
				 <input type="hidden" name="codMatPag[]" value="<?php echo $rsMaterial->Fields('id_material'); ?>" />
				 <input type="hidden" name="detPag[]" value="<?php echo $rsMaterial->Fields('id_detalleprestamo'); ?>" />
				<?php
			}
			?>
		  </td>
		  <input type="hidden" name="pago[]" value="<?php echo $isPago; ?>" />
		 
 		</tr>
		<?php
		$rsMaterial->MoveNext(); 
	  }
	  $rsMaterial->Close();
	?>
	</table>
	<input name="mm_insert2" type="hidden" id="mm_insert2" value="form2">
	<input name="Confirmar" type="submit" id="Confirmar" value="Confirmar" onClick="confirmarReservas()">
</form>
<?php
}
?>

</body>
</html>
<?php

?>
